import { reactive, ref } from 'vue'
import type { FormRules } from 'naive-ui'

export function useForm() {
  const state = reactive({
    loginFormRef: ref(),
    loginForm: {
      username: '',
      password: '',
      grant_type: 'password'
    },
    rules: {
      username: {
        trigger: ['input', 'blur'],
        validator() {
          if (state.loginForm.username === '') {
            return new Error(window.$t('login.usernameTips'))
          }
        }
      },
      password: {
        trigger: ['input', 'blur'],
        validator() {
          if (state.loginForm.password === '') {
            return new Error(window.$t('login.userPasswordTips'))
          }
        }
      }
    } as FormRules
  })

  return {
    state
  }
}
